package com.mak001.lite.multiSkill.subs.Miners;

import org.rsbuddy.tabs.Inventory;
import org.rsbuddy.widgets.Bank;

import extras.mak001.AnimWatcher;
import com.mak001.lite.multiSkill.Methods;
import extras.mak001.Time;
import com.mak001.lite.multiSkill.subScript;
import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.script.methods.Objects;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.methods.Walking;
import com.rsbuddy.script.wrappers.Area;
import com.rsbuddy.script.wrappers.GameObject;

public class Miner extends subScript {

	int pick = 1275;
	int[] rockID;
	Area bankArea;
	Area mineArea;
	boolean isBanking;

	boolean switchB;

	@Override
	public boolean canSwitch() {
		return switchB;
	}

	public enum state {
		BANK, WALKTOBANK, MINE, WALKTOMINE, DROP
	}

	public state getState() {
		if (Inventory.isFull()) {
			if (bankArea.contains(Players.getLocal().getLocation())) {
				return state.BANK;
			} else {
				return state.WALKTOBANK;
			}
		} else {
			if (isBanking == true) {
				if (mineArea.contains(Players.getLocal().getLocation())) {
					return state.MINE;
				} else {
					return state.WALKTOMINE;
				}
			} else {
				return state.DROP;
			}
		}
	}

	@Override
	public void execute() {
		if (bankArea == null) {
			bankArea = Methods.getNearestBank(mineArea.getCentralTile());
		}
		switch (getState()) {
		case BANK:
			setSwitch(true);
			if (Bank.isOpen()) {
				Bank.depositAllExcept(pick);
			} else {
				Bank.open();
			}
			break;
		case MINE:
			if (rock() != null && AnimWatcher.isIdle()
					&& !Players.getLocal().isMoving()) {
				if (rock().isOnScreen()) {
					if (rock().interact("")) {
						sleep(1000);
					}
				} else {
					Walking.findPath(rock().getLocation()).traverse();
				}
			}
			break;
		case WALKTOMINE:
			setSwitch(false);
			if (Bank.isOpen()) {
				Bank.close();
			}
			Walking.findPath(mineArea.getCentralTile()).traverse();
			break;
		case WALKTOBANK:
			Walking.findPath(bankArea.getCentralTile()).traverse();
			break;
		case DROP:
			Inventory.dropAllExcept(pick);
			break;
		}
	}

	public GameObject rock() {
		return Objects.getNearest(rockID);
	}

	public void setSwitch(boolean b) {
		if (switchB != b)
			switchB = b;
	}

	@Override
	public int[] required() {
		int[] req = { pick };
		// TODO Auto-generated method stub
		return req;
	}

	public void setRock(int[] i) {
		rockID = i;
	}

	public void setMine(Area a) {
		mineArea = a;
	}
	
	public void setBanking(boolean b){
		isBanking = b;
	}

	@Override
	public String getStatus() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int skill() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void messageRecieved(MessageEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String[] paintString() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Time getTime() {
		// TODO Auto-generated method stub
		return null;
	}

}
